package org.aeon.client.view.components.sessionPanel;

import org.aeon.client.context.GlobalContext;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.entity.dto.FriendApplyDto;
import org.aeon.entity.dto.UserInfoDto;
import org.aeon.message.request.FriendApplyRequestMessage;
import org.aeon.message.request.GroupJoinRequestMessage;
import org.aeon.utils.SimpleRequestUtil;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/23 15:54
 * @description:
 */
public class RequestDialog extends JDialog {
    private final JPanel panel;
    private final JLabel nicknameLabel;
    private final JLabel usernameLabel;
    private final JTextArea textArea;
    private final JButton submitBut;
    private final JButton cancelBut;

    public RequestDialog(String title) {
        this.setModal(false);
        this.setTitle(title);
        this.setSize(270, 300);
        this.setResizable(false);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        // 设置位置
        this.setLocationRelativeTo(GlobalContext.getContext().getMainUI());

        panel = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        Border paddingBorder = BorderFactory.createEmptyBorder(15, 20, 20, 20);
        panel.setBorder(paddingBorder);

        final Box nicknameBox = Box.createHorizontalBox();
        // 昵称
        nicknameLabel = new JLabel("昵称");
        nicknameLabel.setFont(new Font("黑体", Font.BOLD, 24));
        nicknameBox.add(nicknameLabel);
        nicknameBox.add(Box.createHorizontalGlue());
        panel.add(nicknameBox);

        panel.add(Box.createVerticalStrut(10));

        final Box usernameBox = Box.createHorizontalBox();
        // 用户名
        usernameLabel = new JLabel("用户名");
        usernameLabel.setFont(new Font("黑体", Font.BOLD, 12));
        usernameBox.add(usernameLabel);
        usernameBox.add(Box.createHorizontalGlue());
        panel.add(usernameBox);

        panel.add(Box.createVerticalStrut(15));

        // 填写验证信息
        final Box validateBox = Box.createHorizontalBox();

        final JLabel validateLabel = new JLabel("填写验证信息");
        validateBox.add(validateLabel);
        validateBox.add(Box.createHorizontalGlue());
        panel.add(validateBox);

        panel.add(Box.createVerticalStrut(5));

        // 文本域
        textArea = new JTextArea();
        textArea.setFont(new Font("黑体", Font.PLAIN, 16));
        panel.add(textArea);

        panel.add(Box.createVerticalStrut(40));


        final Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());

        // 发送
        submitBut = new JButton("发送");
        box.add(submitBut);

        box.add(Box.createHorizontalStrut(30));

        // 取消
        cancelBut = new JButton("取消");
        cancelBut.addActionListener(e -> this.dispose());
        box.add(cancelBut);

        box.add(Box.createHorizontalGlue());

        panel.add(box);

        this.getContentPane().add(panel);
    }

    public void fillInData(UserInfoDto user) {
        nicknameLabel.setText(user.getNickname());
        usernameLabel.setText(user.getUsername());

        panel.revalidate();
        panel.repaint();

        submitBut.addActionListener(e -> {
            submitBut.setEnabled(false);
            final GlobalContext context = GlobalContext.getContext();
            // 获取信息发送好友请求
            final FriendApplyRequestMessage reqMsg = new FriendApplyRequestMessage();
            reqMsg.setMessage(new FriendApplyDto(context.getCurrentUser().getId(), user.getId(),
                    textArea.getText(), null, null));
            SimpleRequestUtil.send(context, reqMsg);
            this.dispose();
        });
    }

    public void fillInData(GroupInfoDto groupInfo) {
        nicknameLabel.setText(groupInfo.getGroupName());
        usernameLabel.setText("");

        panel.revalidate();
        panel.repaint();

        submitBut.addActionListener(e -> {
            submitBut.setEnabled(false);
            final GlobalContext context = GlobalContext.getContext();
            final GroupJoinRequestMessage reqMsg = new GroupJoinRequestMessage(context.getCurrentUser().getId(),
                    groupInfo.getId());
            SimpleRequestUtil.send(context, reqMsg);
            this.dispose();
        });
    }
}
